package com.abc.easyclaim;

import java.util.ArrayList;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

public class PosOverLay extends Overlay{

	final static int picWidth=20;  //气球图的宽度
	final static int picHeight=34; //气球图的高度
	final static int arcR=8;//信息窗口的圆角半径
	//定义该PosOverLay所绘制的位图
	Bitmap posBitmap;
	//定义该PosOverLay绘制位图的位置
	GeoPoint geoPoint;
	String locationString;        //显示具体位置的字符串
	
	public PosOverLay(GeoPoint geoPoint,Bitmap posBitmap,String locationString) {
		super();
		this.geoPoint=geoPoint;
		this.posBitmap=posBitmap;
		this.locationString=locationString;
	}
	
	@Override
	public void draw(Canvas arg0, MapView arg1, boolean arg2) {
		// TODO Auto-generated method stub
		if (!arg2) {
			//获取MapView的Projection对象
			Projection projection=arg1.getProjection();
			Point point=new Point();
			//将真实的地理坐标转换为屏幕上的坐标
			projection.toPixels(geoPoint, point);
			//在指定位置绘制图片
			arg0.drawBitmap(posBitmap, point.x-posBitmap.getWidth()/2, point.y-posBitmap.getHeight(), null);
			//绘制信息窗口
			drawWindow(arg0,point,250);
		}
		super.draw(arg0, arg1, arg2);
	}

	public void drawWindow(Canvas canvas,Point point,int winWindth) {
		int charSize=15;          //字符大小   
		int textSize=16;        //文字大小
		int leftRightPadding=2;          //留白间距
		int lineWidth=winWindth-leftRightPadding*2;          //每行的宽度
		int lineCharCount=lineWidth/(charSize);        //每行字符数
		ArrayList<String> aList=new ArrayList<String>();  //记录所有行的ArrayList
		String currentRow="";               //当前行的字符串
		for (int i = 0; i < locationString.length(); i++) {
			char c=locationString.charAt(i);
			if(c!='\n'){                //若当前字符不是换行符
				currentRow +=c;
			}
			else {
				if (currentRow.length()>0) {            //若长度大于0
					aList.add(currentRow);              //加入列表中
				}
				currentRow="";            //清空当前行
			}
			if (currentRow.length()==lineCharCount) {     //一行的字符数满后，自动换行
				aList.add(currentRow);
				currentRow="";
			}
		}
		if (currentRow.length()>0){             //若当前行长度大于0，将当前行加入记录所有行的ArrayList
			aList.add(currentRow);
		}
		int lineCount=aList.size();                    //获得总行数
		int winHeight=lineCount*(charSize)+2*arcR;          //自动计算信息窗体的高度
		Paint paint=new Paint();                  //创建Paint对象
		paint.setAntiAlias(true);          //打开抗锯齿
		paint.setTextSize(textSize);           //设置文字大小
		//绘制信息窗体圆角矩形
		paint.setARGB(255, 255, 251, 239);      
		int x1=point.x-winWindth/2;
		int y1=point.y-picHeight-winHeight-1;
		int x2=x1+winWindth;                //设定起始位置
		int y2=y1+winHeight;
		RectF rectF=new RectF(x1, y1, x2, y2);            //绘制矩形
		canvas.drawRoundRect(rectF, arcR, arcR, paint);
		//绘制信息窗体圆角矩形边框
		paint.setARGB(255, 198, 195, 198);
		paint.setStyle(Paint.Style.STROKE);        //设置字体
		paint.setStrokeWidth(2);
		canvas.drawRoundRect(rectF, arcR, arcR, paint);
		//循环绘制每行文字
		paint.setStrokeWidth(0);
		paint.setARGB(255, 10, 10, 10);
		int lineY=y1+arcR+charSize;
		for (String lineString : aList) {        //对每一行进行循环
			for (int j = 0; j < lineString.length(); j++) {      //对一行中的每个字循环
				String colString=lineString.charAt(j)+"";
				canvas.drawText(colString, x1+leftRightPadding+j*charSize, lineY, paint);
			}
			lineY +=charSize;            //Y坐标移向下一行
		}
	}

}
